package com.das.mechanic_base.widget;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.app.X3RouterCenter;
import com.das.mechanic_base.utils.SpHelper;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by chenSi on 2018/3/19.
 */

public class X3CancelPopUpWindow implements View.OnClickListener, PopupWindow.OnDismissListener {

    private PopupWindow popupWindow;

    private Activity mContext;
    private long receiveBaseId;
    private long workBaseId;
    private TextView tv_work;
    private TextView tv_cancel;
    private TextView tv_camera;
    private int type;// 0 检测单 1 施工单
    private TextView tv_video;
    private ImageView iv_red;
    private RelativeLayout rl_live;
    private boolean isshowred = false;
    private TextView tv_look;
    private boolean isSdk;

    public X3CancelPopUpWindow(Activity mContext) {
        this.mContext = mContext;

    }

    public void showPopupWindow(View v,boolean isshowred) {
        this.isshowred = isshowred;
        //防止重复按按钮
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }
        //设置PopupWindow的View
        View view = LayoutInflater.from(mContext).inflate(R.layout.x3_cancel_view, new LinearLayout(mContext),false);
        popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置背景,这个没什么效果，不添加会报错
        popupWindow.setBackgroundDrawable(null);
        //设置点击弹窗外隐藏自身
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        //设置动画
        //popupWindow.setAnimationStyle(R.style.PopupWindow1);
        //设置位置
        popupWindow.showAsDropDown(v);
        //设置消失监听
        popupWindow.setOnDismissListener(this);
        //设置PopupWindow的View点击事件
        setOnPopupViewClick(view);
        //设置背景色
        setBackgroundAlpha(0.8f);
    }

    private void setOnPopupViewClick(View view) {
        isSdk = (boolean) SpHelper.getData(X3Constent.IS_OPEN_SDK, false);

        tv_cancel = view.findViewById(R.id.tv_cancel);
        rl_live = view.findViewById(R.id.rl_live);
        tv_work = view.findViewById(R.id.tv_work);
        tv_look = view.findViewById(R.id.tv_look);
        tv_camera = view.findViewById(R.id.tv_camera);
        tv_video = view.findViewById(R.id.tv_video);
        iv_red = view.findViewById(R.id.iv_red);
        if(isshowred){
            iv_red.setVisibility(View.VISIBLE);
        }else {
            iv_red.setVisibility(View.GONE);
        }
        tv_cancel.setOnClickListener(this);
        tv_look.setOnClickListener(this);
        tv_work.setOnClickListener(this);
        tv_camera.setOnClickListener(this);
        tv_video.setOnClickListener(this);

        rl_live.setVisibility(isSdk ? View.GONE : View.VISIBLE);

    }

    //设置屏幕背景透明效果
    public void setBackgroundAlpha(float alpha) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = alpha;
        if (alpha != 1) {
            mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }else {
            mContext.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        mContext.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_cancel) {
            if (iOnItemCameraClick != null) {
                onDismiss();
                iOnItemCameraClick.iOnClick();
            }
        } else if (id == R.id.tv_look) {// 查看接车单
            onDismiss();
            X3RouterCenter.startReceiveOrder(receiveBaseId,0,false,false);
        } else if (id == R.id.tv_work) {
            /**
             * TODO 查看施工单详情
             */
            if (iOnItemCameraClick != null) {
                iOnItemCameraClick.iOnJumpToWorkDetail();
            }
            onDismiss();

        } else if (id == R.id.tv_camera) {
            onDismiss();
            Bundle bundle = new Bundle();
            bundle.putInt("type",type);
            bundle.putLong("workBaseId",workBaseId);
            X3RouterCenter.startNavigate(X3AppRouterConstants.Router.QUICK_CAMERA,bundle);

        } else if (id == R.id.tv_video) {
            onDismiss();

            Bundle bundle = new Bundle();
            bundle.putLong("workBaseId",workBaseId);
            X3RouterCenter.startNavigate(X3AppRouterConstants.Router.UPLOAD_VIDEO,bundle);
        }
    }

    /**
     * 传入施工单状态 未开始 施工中 已经结束
     * @param status
     */
    public void showWorkStatus(long status) {
        if (status > 1) {
            tv_cancel.setVisibility(View.GONE);
        }
    }

    public void providerReceive (long receiveBaseId,long workBaseId,int type) {
        this.receiveBaseId = receiveBaseId;
        this.workBaseId = workBaseId;
        this.type = type;
        MobclickAgent.onPageStart(type == 0? "检测单功能提示弹窗" : "施工单功能提示弹窗");
        if (tv_look != null) {
            tv_look.setVisibility((receiveBaseId == 0) ? View.GONE : View.VISIBLE);
        }
    }

    IOpenCameraClick iOnItemCameraClick;

    public void setiOnItemCameraClick(IOpenCameraClick iOnItemCameraClick) {
        this.iOnItemCameraClick = iOnItemCameraClick;
    }

    public interface IOpenCameraClick {
        void iOnClick();

        void iOnJumpToWorkDetail();
    }

    @Override
    public void onDismiss() {
        MobclickAgent.onPageEnd(type == 0? "检测单功能提示弹窗" : "施工单功能提示弹窗");
        setBackgroundAlpha(1);
        popupWindow.dismiss();

    }
}
